package com.jnu.HomeworkApplication;

import android.os.Bundle;
import android.os.Message;

import com.baidu.mapapi.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ShopLoader {

    private URL url;

    public void setUrl(URL url) {
        this.url = url;
    }

    public Message download() throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setUseCaches(false);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {

            InputStream inputStream = httpURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String receive;
            StringBuffer stringBuffer = new StringBuffer();
            while ((receive = bufferedReader.readLine()) != null) {
                stringBuffer.append(receive);
            }
            Message message = new Message();
            message.what = 0;
            Bundle bundle = new Bundle();
            bundle.putString("data", stringBuffer.toString());
            message.setData(bundle);

            return message;
        }
        return null;
    }

    public List<LatLng> parsonJson(String content) {
        List<LatLng> shopPoint = new ArrayList<>();
        if (null != content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray shops = jsonObject.getJSONArray("shops");
                for (int index = 0; index < shops.length(); index++) {
                    JSONObject shop = shops.getJSONObject(index);
                    LatLng centerPoint = new LatLng(shop.getDouble("latitude"), shop.getDouble("longitude"));
                    shopPoint.add(centerPoint);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        else {
            return null;
        }
        return shopPoint;
    }

    public List<String> getShopName(String content){
        List<String> shopName = new ArrayList<>();
        if (null != content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray shops = jsonObject.getJSONArray("shops");
                for (int index = 0; index < shops.length(); index++) {
                    JSONObject shop = shops.getJSONObject(index);
                    String shopProject = shop.getString("name");
                    shopName.add(shopProject);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        else {
            return null;
        }
        return shopName;
    }

}